package com.chatifyme;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class RegisterNewUser extends BaseActivity {
	public XMPPConnection xConn;
	public NetworkConnection nConn;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registration);
		
		 Button signup = (Button) this.findViewById(R.id.btnSignup);
		 signup.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View view) {
	            	RegisterUser();
	            }
	        });
		
	}
	public int PICK_CONTACT=54;
	public void RegisterUser()
	{
		String userName=getEditTextValue(R.id.regUserName);
		String password=getEditTextValue(R.id.regUserName);
		try {

			NetworkConnection nConn=new NetworkConnection(null);
			 	//nConn.ConnectOnly(RegisterNewUser.this); 
			 	//xConn=NetworkConnection.connection;
			 	
			    Log.i ("Connected", "Connect");
			    org.jivesoftware.smack.AccountManager manager = xConn.getAccountManager();
			    
			    if (manager.supportsAccountCreation ())
			    {
			    	manager.createAccount (userName, password);
			    }
			    getContactList();
		
		SessionMaster.StoreUserInfo(userName, password, RegisterNewUser.this);
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
	
	
	

	public void getContactList()
	{
		
		ArrayList<ContactNumber> cNumList=new ArrayList<ContactNumber>();
		ContactNumber cNumber;
		ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
	    while (cur.moveToNext()) {
	        String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
		String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
 		if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
 		    //Query phone here.  Covered next
            if (Integer.parseInt(cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                 Cursor pCur = cr.query(
  		    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
  		    null, 
  		    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
  		    new String[]{id}, null);
                 int phID=0;
  	        while (pCur.moveToNext()) {
  	        	cNumber=new ContactNumber();
  	        	cNumber.myNumber=pCur.getString(phID);
  	        	cNumber.myName=name;
  	        	phID++;
  	        	cNumList.add(cNumber);
  		    // Do something with phones
  	        } 
  	        pCur.close();
  	    }


 	        }
	    }
        }
	}

}
